/**
 * @author Tin Huynh
 *
 */
package uit.tkorg.coauthorgraph.bo;

import uit.tkorg.coauthorgraph.dbaccess.AuthorInstanceMapper;
import uit.tkorg.coauthorgraph.dbaccess.AuthorMapper;
import uit.tkorg.coauthorgraph.dto.AuthorDTO;

public class AuthorBO {
    /**
     * insert a new author into the DB
     * @param authorDTO
     * @throws Exception
     */
    public int insertNewAuthor(AuthorDTO authorDTO) throws Exception {
        int newAuthorID = -1;
        AuthorMapper mapper = null;
        AuthorInstanceMapper authorInstanceMapper = new AuthorInstanceMapper();
        try {
            mapper = new AuthorMapper();
            if (!mapper.isAuthorExisted(authorDTO)) {
                newAuthorID = mapper.insertNewAuthor(authorDTO);
                authorInstanceMapper.insertNewAuthorInstance(newAuthorID, authorDTO.getAuthorName());
            }
            return newAuthorID;
        }
        catch (Exception e) {
            throw e;
        }
        finally {
            mapper.closeConnection();
            authorInstanceMapper.closeConnection();
        }
    }
	
    /**
     * get authorID from authorName
     * @param authorName
     * @return
     * @throws Exception
     */
    public int getAuthorID(String authorName) throws Exception {
        AuthorMapper mapper = null;
        try {
            mapper = new AuthorMapper();
            return mapper.getAuthorID(authorName);
        }
        catch (Exception e) {
            throw e;
        }
        finally {
            mapper.closeConnection();
        }
    }
}
